package models

import (
	"time"
)

type MindmapAi struct {
	Model
	ID               int    `gorm:"column:id"`
	TaskId           string `gorm:"uniqueIndex;column:task_id;not null;comment:任务ID"`
	MindId           string `gorm:"default:0;column:mind_id"`
	UserId           int    `gorm:"not null;column:acid;comment:用户ID"`
	Question         string `gorm:"type:text;column:question;comment:用户问题"`
	PromptTokens     int    `gorm:"default:0;column:prompt_tokens;comment:输入Token数"`
	CompletionTokens int    `gorm:"default:0;column:completion_tokens;comment:输出Token数"`
	TotalTokens      int    `gorm:"default:0;column:total_tokens;comment:总消耗Token数"`
	Status           int    `gorm:"default:0;column:status;"`
	Lang             string `gorm:"default:0;column:lang;"`
	EndTime          int64  `gorm:"default:0;column:end_time;"`
	LangTime         int    `gorm:"default:0;column:lang_time;"`
	Error            string `gorm:"default:0;column:error;"`
	ModelType        string `gorm:"default:0;column:model_type;"`
	UserStatus       int    `gorm:"default:0;column:user_status;"`
	FrontCredit      int    `gorm:"default:0;column:front_credit;"`
	AfterCredit      int    `gorm:"default:0;column:after_credit;"`
	CreateTime       int64  `gorm:"autoCreateTime;column:create_time;comment:创建时间"`
	UpdateTime       int64  `gorm:"autoUpdateTime;column:update_time;comment:更新时间"`
}

func CreateAiLog(data map[string]interface{}) bool {
	timestamp := time.Now().Unix()
	db.Create(&MindmapAi{
		TaskId:     data["taskId"].(string),
		UserId:     data["user_id"].(int),
		Question:   data["question"].(string),
		CreateTime: timestamp,
		UpdateTime: timestamp,
		Status:     0,
	})
	return true
}

func UpdateAiLog(data map[string]interface{}, task_id string) bool {
	db.Model(&MindmapAi{}).Where("task_id = ?", task_id).Update(data)
	return true
}

func ShowAiLog(task_id string) (mindmap MindmapAi) {
	db.Where("task_id = ?", task_id).First(&mindmap)
	return
}
